package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryResultDTO;
import com.neusoft.neuiotms.dcam.fixed.model.InventoryResult;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryDutyResultStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.InventoryDutyRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.InventoryResultRepository;
import com.neusoft.neuiotms.dcam.fixed.vo.InventoryResultVO;

@Component
public class InventoryResultConverter extends AbstractConverter<InventoryResult, InventoryResultDTO> {

    @Autowired
    private InventoryDutyRepository inventoryDutyRepository;
    @Autowired
    private InventoryResultRepository inventoryDutyResultRepository;
    @Autowired
    private AssetTypeRepository assetTypeRepository;

    @Override
    public InventoryResult toModel(final InventoryResultDTO dto) {

        InventoryResult model;
        if (dto.isNew()) {
            model = new InventoryResult();
            model.setName(dto.getName());
            model.setCode(dto.getCode());
            model.setSerialnumber(dto.getSerialnumber());
            model.setType(this.assetTypeRepository.findByCode(dto.getType()));
            model.setContractNo(dto.getContractNo());
            model.setLocationCode(dto.getLocationCode());
            model.setLocationName(dto.getLocationName());
            model.setStatus(dto.getStatus());
            model.setUserId(dto.getUserId());
            model.setUserName(dto.getUserName());
            model.setOrgId(dto.getOrgId());
            model.setOrgName(dto.getOrgName());
            model.setInventoryDuty(this.inventoryDutyRepository.getOne(dto.getDutyId()));
        } else {
            // 更新时，只能改位置、部门、使用人
            // 查询原始数据
            model = this.inventoryDutyResultRepository.getOne(dto.getId());
            // 变更位置
            model.setUpdateLocationCode(dto.getUpdateLocationCode());
            model.setUpdateLocationName(dto.getUpdateLocationName());
            // 变更部门
            model.setUpdateOrgCode(dto.getUpdateOrgCode());
            model.setUpdateOrgName(dto.getUpdateOrgName());
            // 变更使用人
            model.setUpdateUserId(dto.getUpdateUserId());
            model.setUpdateUserName(dto.getUpdateUserName());
        }
        model.setAssetId(dto.getAssetId());
        // 盘点结果
        model.setDutyResultStatus(dto.getDutyResultStatus());
        model.setNote(dto.getNote());
        model.setUpdateTime(new Date());

        return model;
    }

    @Override
    public InventoryResultDTO toDTO(final InventoryResult model, final boolean forListView) {
        final InventoryResultDTO dto = new InventoryResultDTO();
        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setSerialnumber(model.getSerialnumber());
        dto.setType(model.getType().getCode());
        dto.setContractNo(model.getContractNo());
        dto.setLocationCode(model.getLocationCode());
        dto.setLocationName(model.getLocationName());
        dto.setStatus(model.getStatus());
        dto.setUserId(model.getUserId());
        dto.setUserName(model.getUserName());
        dto.setOrgId(model.getOrgId());
        dto.setOrgName(model.getOrgName());
        dto.setDutyId(model.getInventoryDuty().getId());
        dto.setDutyName(model.getInventoryDuty().getName());
        dto.setPlanId(model.getInventoryDuty().getInventoryPlan().getId());
        dto.setPlanName(model.getInventoryDuty().getInventoryPlan().getName());
        dto.setDutyResultStatus(model.getDutyResultStatus());
        dto.setUpdateLocationCode(model.getUpdateLocationCode());
        dto.setUpdateLocationName(model.getUpdateLocationName());
        dto.setUpdateOrgCode(model.getUpdateOrgCode());
        dto.setUpdateOrgName(model.getUpdateOrgName());
        dto.setUpdateUserId(model.getUpdateUserId());
        dto.setUpdateUserName(model.getUpdateUserName());
        dto.setNote(model.getNote());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setAssetId(model.getAssetId());
        return dto;
    }

    /**
     * 从资产复制到盘点结果
     *
     * @param source
     * @param dutyId
     * @return
     */
    public InventoryResultDTO toDTO(final AssetDTO source, final Long dutyId) {
        final InventoryResultDTO dto = new InventoryResultDTO();
        dto.setCode(source.getCode());
        dto.setName(source.getName());
        dto.setSerialnumber(source.getSerialnumber());
        dto.setType(source.getSmallTypeCode());
        dto.setContractNo(source.getContractNo());
        dto.setLocationCode(source.getLocation());
        dto.setLocationName(source.getLocationName());
        dto.setUserId(source.getManagementUserCode());
        dto.setUserName(source.getManagementUserName());
        dto.setOrgId(source.getManagementOrgCode());
        dto.setOrgName(source.getManagementOrgName());
        // 盘点状态初始化为待盘点
        dto.setDutyResultStatus(InventoryDutyResultStatus.wait);
        dto.setDutyId(dutyId);
        dto.setAssetId(source.getId());
        return dto;
    }

    /**
     * 盘点结果实体转换成盘点结果Excel记录项
     *
     * @param model
     * @return
     */
    public InventoryResultVO toVO(final InventoryResult model) {
        final InventoryResultVO vo = new InventoryResultVO();
        vo.setCode(model.getCode());
        vo.setName(model.getName());
        vo.setResultStatus(model.getDutyResultStatus());
        vo.setLocationName(model.getLocationName());
        vo.setUserName(model.getUserName());
        vo.setOrgName(model.getOrgName());
        vo.setUpdateLocationName(model.getUpdateLocationName());
        vo.setUpdateUserName(model.getUpdateUserName());
        vo.setUpdateOrgName(model.getUpdateOrgName());
        vo.setUpdateTime(model.getUpdateTime());
        return vo;
    }

    public AssetDTO toDTO(final InventoryResultDTO source) {
        final AssetDTO dto = new AssetDTO();
        return dto;

    }

    public InventoryResult toModel(final InventoryResultVO vo) {
        final InventoryResult model = new InventoryResult();
        return model;
    }
}
